package com.avic.szzx.controller.TestController;

import com.avic.szzx.service.MysqlService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class TaskController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private MysqlService mysqlService;

    // 同步发送（基础版）
    @GetMapping("/rocketmq/normal/{message}")
    public String sendNormalMessage(@PathVariable("message") String message) {
        rocketMQTemplate.convertAndSend("test-topic", message);
        return "消息已发送: " + message;
    }

    @GetMapping("/task22")
    public String getData22(){
        String value = mysqlService.getData();
        System.out.println("value：" + value);
        String tag = "processing_record2";
        value = "{\n" +
                "  \"tag\": \"processing_record2\",\n" +
                "  \"option\": \"ADD\",\n" +
                "  \"name\": \"name122222222222222\",\n" +
                "  \"process_name\":  \"装配1\",\n" +
                "  \"process_document\": \"工艺指导书1\"\n" +
                "}";

        rocketMQTemplate.convertAndSend("TestTopic:" + tag, value);
        return value;
    }

    @GetMapping("/task2222")
    public String getData(){
        System.out.println("get1111111111111");
        String value = mysqlService.getData();
        return value;
    }

    @GetMapping("/task/one/processing_record3")
    public String getDataOne(){
//        String table = "processing_record3";
        String tag = "processing_record3";
        System.out.println("tag：" + tag);
        String table = tag;
        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        List<Map<String, Object>> map = mysqlService.getDataList(table);
        for (Map<String, Object> entry : map) {
            System.out.println("entry：" + entry.toString());
            String jsonStr = gson.toJson(entry);
            System.out.println("\n\n\nJSON格式输出：\n" + jsonStr);

            entry.put("tag", table);
            entry.put("option", "ADD");

            jsonStr = gson.toJson(entry);
            System.out.println("\n\n\nJSON格式输出222：\n" + jsonStr);

            rocketMQTemplate.convertAndSend("TestTopic:" + table, jsonStr);
            break;
        }
        return "success!";
    }
//
//    @GetMapping("/task/one/{tag}")
//    public String getDataOne2(@PathVariable("tag") String tag){
////        String table = "processing_record3";
//        System.out.println("tag：" + tag);
//        String table = tag;
//        Gson gson = new GsonBuilder().setPrettyPrinting().create();
//
//        List<Map<String, Object>> map = mysqlService.getDataList(table);
//        for (Map<String, Object> entry : map) {
//            System.out.println("entry：" + entry.toString());
//            String jsonStr = gson.toJson(entry);
//            System.out.println("\n\n\nJSON格式输出：\n" + jsonStr);
//
//            entry.put("tag", table);
//            entry.put("option", "ADD");
//
//            jsonStr = gson.toJson(entry);
//            System.out.println("\n\n\nJSON格式输出222：\n" + jsonStr);
//
//            rocketMQTemplate.convertAndSend("TestTopic:" + table, jsonStr);
//            break;
//        }
//        return "success!";
//    }

    @GetMapping("/task/processing_record3")
    public String getDataAll(){
        String tag = "processing_record3";
    //        String table = "processing_record3";
        System.out.println("tag：" + tag);
        String table = tag;
        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        List<Map<String, Object>> map = mysqlService.getDataList(table);
        for (Map<String, Object> entry : map) {
            System.out.println("entry：" + entry.toString());
            String jsonStr = gson.toJson(entry);
            System.out.println("\n\n\nJSON格式输出：\n" + jsonStr);

            entry.put("tag", table);
            entry.put("option", "ADD");

            jsonStr = gson.toJson(entry);
            System.out.println("\n\n\nJSON格式输出222：\n" + jsonStr);

            rocketMQTemplate.convertAndSend("TestTopic:" + table, jsonStr);
        }
        return "success!";
    }


//    @GetMapping("/task/{tag}")
//    public String getDataAll(@PathVariable("tag") String tag){
////        String table = "processing_record3";
//        System.out.println("tag：" + tag);
//        String table = tag;
//        Gson gson = new GsonBuilder().setPrettyPrinting().create();
//
//        List<Map<String, Object>> map = mysqlService.getDataList(table);
//        for (Map<String, Object> entry : map) {
//            System.out.println("entry：" + entry.toString());
//            String jsonStr = gson.toJson(entry);
//            System.out.println("\n\n\nJSON格式输出：\n" + jsonStr);
//
//            entry.put("tag", table);
//            entry.put("option", "ADD");
//
//            jsonStr = gson.toJson(entry);
//            System.out.println("\n\n\nJSON格式输出222：\n" + jsonStr);
//
//            rocketMQTemplate.convertAndSend("TestTopic:" + table, jsonStr);
//        }
//        return "success!";
//    }


    @GetMapping("/task/edge/equipment_process_relationship2")
    public String getDataEdgeAll(){
        String tag = "equipment_process_relationship2";
        System.out.println("tag：" + tag);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        List<Map<String, Object>> map = mysqlService.getDataEdgeList(tag);
        for (Map<String, Object> entry : map) {
            System.out.println("entry：" + entry.toString());
            String jsonStr = gson.toJson(entry);
            System.out.println("\n\n\nJSON格式输出：\n" + jsonStr);

            entry.put("tag", tag);
            entry.put("option", "ADD");

            jsonStr = gson.toJson(entry);
            System.out.println("\n\n\nJSON格式输出222：\n" + jsonStr);

            rocketMQTemplate.convertAndSend("test_edge:" + tag, jsonStr);
            break;
        }
        return "success!";
    }

//
//    @GetMapping("/task/edge/{tag}")
//    public String getDataEdgeAll(@PathVariable("tag") String tag){
//        System.out.println("tag：" + tag);
//        Gson gson = new GsonBuilder().setPrettyPrinting().create();
//
//        List<Map<String, Object>> map = mysqlService.getDataEdgeList(tag);
//        for (Map<String, Object> entry : map) {
//            System.out.println("entry：" + entry.toString());
//            String jsonStr = gson.toJson(entry);
//            System.out.println("\n\n\nJSON格式输出：\n" + jsonStr);
//
//            entry.put("tag", tag);
//            entry.put("option", "ADD");
//
//            jsonStr = gson.toJson(entry);
//            System.out.println("\n\n\nJSON格式输出222：\n" + jsonStr);
//
//            rocketMQTemplate.convertAndSend("test_edge:" + tag, jsonStr);
//            break;
//        }
//        return "success!";
//    }


    // 带Tag的同步发送（推荐）
    @GetMapping("/rocketmq/withTag/{tag}/{message}")
    public String sendWithTag(@PathVariable("tag") String tag,
                              @PathVariable("message") String message) {
        rocketMQTemplate.convertAndSend("test-topic:" + tag, message);
        return "带Tag消息已发送: " + message;
    }

    // 异步发送（带回调）
    @GetMapping("/rocketmq/async/{message}")
    public String sendAsync(@PathVariable("message") String message) {
        rocketMQTemplate.asyncSend("test-topic", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("异步发送成功: " + sendResult);
            }
            @Override
            public void onException(Throwable e) {
                System.out.println("异步发送失败: " + e.getMessage());
            }
        });
        return "异步消息已提交: " + message;
    }
}